package com.ruoyi.common.utils;

import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Slf4j
public class AttachmentWriteHandler implements RowWriteHandler {

    private String attachmentFilePath;
    private String targetColumnName; // 目标列名

    public AttachmentWriteHandler(String attachmentFilePath, String targetColumnName) {
        this.attachmentFilePath = attachmentFilePath;
        this.targetColumnName = targetColumnName;
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        if (!isHead && isAttachmentRow(row, writeSheetHolder.getSheet())) {
            Sheet sheet = writeSheetHolder.getSheet();
            Workbook workbook = sheet.getWorkbook();
            CreationHelper creationHelper = workbook.getCreationHelper();

            Row headerRow = sheet.getRow(2);
            int attachmentColumnIndex = -1;
            int columnCount = headerRow.getLastCellNum();
            for (int i = 0; i < columnCount; i++) {
                Cell cell = headerRow.getCell(i);
                if (cell != null && targetColumnName.equals(cell.getStringCellValue())) {
                    attachmentColumnIndex = i;
                    break;
                }
            }

            if (attachmentColumnIndex != -1) {
                Cell cell = row.getCell(attachmentColumnIndex);
                if (cell != null) {
                    // 创建超链接
                    Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.FILE);
                    log.info("attachmentFilePath: " + attachmentFilePath);
                    String filePath = Paths.get(attachmentFilePath).toUri().toString();
                    hyperlink.setAddress(filePath); // 设置文件路径
                    log.info("filePath: " + filePath);

                    // 设置超链接样式
                    CellStyle hyperlinkStyle = workbook.createCellStyle();
                    Font font = workbook.createFont();
                    font.setColor(IndexedColors.BLUE.getIndex()); // 设置字体颜色为蓝色
                    font.setUnderline(Font.U_SINGLE); // 增加下划线
                    hyperlinkStyle.setFont(font);
                    hyperlinkStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中对齐
                    hyperlinkStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中对齐
                    hyperlinkStyle.setBorderBottom(BorderStyle.THIN);
                    cell.setCellStyle(hyperlinkStyle); // 应用样式到单元格

                    // 将超链接应用于单元格
                    cell.setHyperlink(hyperlink);
                }
            }
        }
    }

    // 检查最后一个单元格是否为指定列名
    private boolean isAttachmentRow(Row row, Sheet sheet) {
        // 检查是否为最后一行
        int lastRowNum = sheet.getLastRowNum();
        if (row.getRowNum() != lastRowNum) {
            return false;
        }

        // 检查是否包含指定的列头“备注”
        Row headerRow = sheet.getRow(2); // 列头在第3行
        int columnCount = headerRow.getLastCellNum();
        for (int i = 0; i < columnCount; i++) {
            Cell cell = headerRow.getCell(i);
            if (cell != null && cell.getStringCellValue().equals("备注")) {
                // 获取指定列的单元格
                Cell attachmentCell = row.getCell(i);
                if (attachmentCell != null) {
                    return true;
                }
            }
        }
        return false;
    }
}

